/*
* 用户权限相关公用函数
* */
import Cookies from "js-cookie";
import LocalStorage from "localStorage";

const TokenKey = "Admin-Token";
const UserKey = "User-Token";

export function getToken() {
    return Cookies.get(TokenKey);
}

export function setToken(token) {
    return Cookies.set(TokenKey, token);
}

export function getUserInfo() {
    let userInfo = LocalStorage.getItem(UserKey);
    if (userInfo) userInfo = JSON.parse(userInfo);
    return userInfo;
}

export function setUserInfo(userInfo) {
    return LocalStorage.setItem(UserKey, JSON.stringify(userInfo));
}

export function removeToken() {
    return Cookies.remove(TokenKey);
}

export function removeUserInfo() {
    return LocalStorage.removeItem(UserKey);
}

export function getRouters() {
    let routes = getUserInfo().routes;
    let routerArr = [];
    routes.forEach(function (panertRoute) {
        let router = {
            path: "/" + panertRoute.path,
            name: panertRoute.name,
            component: () => import("@/layout"),
            children: []
        };
        let mobileRouter = {
            path: "/mobile/" + panertRoute.path,
            name: "mobile-" + panertRoute.name,
            component: () => import("@/layout"),
            children: []
        };

        let childrens = panertRoute.children;
        childrens.forEach(function (childRoute) {
            let childrenObj = {
                path: childRoute.path,
                name: childRoute.name,
                component: () => import("@/views/" + childRoute.url)
            };
            let mobileChildrenObj = {
                path: childRoute.path,
                name: "mobile-" + childRoute.name,
                component: () => import("@/views/mobile/" + childRoute.url)
            };
            router.children.push(childrenObj);
            mobileRouter.children.push(mobileChildrenObj);
        });
        routerArr.push(router);
        routerArr.push(mobileRouter);
    });
    return routerArr;
}
